package com.moji.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
public class MoJiCommonUtils {

    private static final String SECRET = "my_secret";

    public static HttpServletRequest getRequestContextHolderRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    }

    public static String getUserByToken() {
        String token = getRequestContextHolderRequest().getHeader("Authorization");
        log.info("获取登录用户Token相关信息=={}", token);
        return token;
    }

    public static String getStringUserInfo(String key) {
        log.info("获取String用户信息=={}", key);
        Map<String, Claim> stringClaimMap = verifyToken(getUserByToken());
        Claim keyValue = (Claim)stringClaimMap.get(key);
        return keyValue == null ? "" : keyValue.asString();
    }

    public static Map<String, Claim> getUserMapInfo() {
        return verifyToken(getUserByToken());
    }

    public static Map<String, Claim> verifyToken(String token) {
        DecodedJWT jwt = null;

        try {
            JWTVerifier verifier = JWT.require(Algorithm.HMAC256("my_secret")).acceptLeeway(2L).build();
            jwt = verifier.verify(token);
        } catch (Exception var3) {
            log.error(var3.getMessage());
            log.error("token解码异常");
            return null;
        }

        return jwt.getClaims();
    }

    public static String getUserCode() {
        Map<String, Claim> stringClaimMap = verifyToken(getUserByToken());
        return stringClaimMap != null && stringClaimMap.get("userCode") != null ? ((Claim)stringClaimMap.get("userCode")).asString() : "无用户";
    }

    public static String getUserPhone() {
        Map<String, Claim> stringClaimMap = verifyToken(getUserByToken());
        return stringClaimMap != null && stringClaimMap.get("mobile") != null ? ((Claim)stringClaimMap.get("mobile")).asString() : "无用户";
    }

    public static String getLoginId() {
        Map<String, Claim> stringClaimMap = verifyToken(getUserByToken());
        return stringClaimMap != null && stringClaimMap.get("loginId") != null ? ((Claim)stringClaimMap.get("loginId")).asString() : "无账号";
    }

    public static String getUserName() {
        Map<String, Claim> stringClaimMap = verifyToken(getUserByToken());
        return stringClaimMap != null && stringClaimMap.get("userName") != null ? ((Claim)stringClaimMap.get("userName")).asString() : "无名称";
    }

    public static String getRoleCode() {
        Map<String, Claim> stringClaimMap = verifyToken(getUserByToken());
        return stringClaimMap != null && stringClaimMap.get("roleCode") != null ? ((Claim)stringClaimMap.get("roleCode")).asString() : "无名称";
    }

    public static String getDeptCode() {
        Map<String, Claim> stringClaimMap = verifyToken(getUserByToken());
        return stringClaimMap != null && stringClaimMap.get("deptCode") != null ? ((Claim)stringClaimMap.get("deptCode")).asString() : "无部门";
    }
}
